<?php

namespace app\admin\model;

use think\Model;

/**
 * Product
 */
class Product extends Model
{
    // 表名
    protected $name = 'product';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 追加属性
    protected $append = [
        'progy',
        'probz',
        'prosm',
    ];

    protected static function onAfterInsert($model): void
    {
        if (is_null($model->weigh)) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getProgyAttr($value, $row): array
    {
        return [
            'name' => \app\admin\model\Progy::whereIn('id', $row['progy_ids'])->column('name'),
        ];
    }

    public function getProgyIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setProgyIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getProbzAttr($value, $row): array
    {
        return [
            'name' => \app\admin\model\Probz::whereIn('id', $row['probz_ids'])->column('name'),
        ];
    }

    public function getProbzIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setProbzIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getProsmAttr($value, $row): array
    {
        return [
            'name' => \app\admin\model\Prosm::whereIn('id', $row['prosm_ids'])->column('name'),
        ];
    }

    public function getProsmIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setProsmIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getContentAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getXijieAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setXijieAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getShoucangAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getFahuoAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function protype(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Protype::class, 'protype_id', 'id');
    }

    public function artist(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Artist::class, 'artist_id', 'id');
    }
}